
%macro print_listings(program=, disname=, idisname=, ds_list=, where=, bn=, ttt=, ftnote=, emp=);

%get_data(pre=adam,lib=adam,supp=n,lst=adlb);

data _adamadlb(keep=subjid usubjid saffl parcat1 parcat1n param paramcd paramn avalc aval ADT ATM ANRLO ANRHI anrange LBCLSIG anrind ablfl ANL01FL adtm  avisitn avisit visit visitnum visityp);
set adamadlb(where=(ady > 0));
run;

data lb;
/*length range $20.;*/
  set _adamadlb;
  where SAFFL = 'Y' and LBCLSIG in ("NCS") /*and anl01fl = 'Y' */;
/*  where &where and index(avisit,"Scr")=0 and  index(avisit,"Post")=0 and index(avisit,"End")=0 and index(avisit,"P1 Day -1")=0;*/
    *where &where and index(visit,"Scr")=0 and index(visit,"Day -1")=0;

DATE=PUT(ADT,YYMMDD10.);
TIME=PUT(ATM,TIME5.);
if length(strip(time)) < 5 then time = "0" ||strip(time); 
else time = trim(time); 
LBDTC=TRIM(DATE) || "/" || TRIM(TIME);

  
  param = "  " || strip(param);
/*  %concat_var_listc(var_res=paramc,varlist=parcat1 param, delimiter=%str(/    \par \li200 ),length_var_Res=$200., test_empty=No,deleteIfEmpty=Yes);*/
param = tranwrd(param, "10^3", "10^{super 3}");
param = tranwrd(param, "10^6", "10^{super 6}");
param = tranwrd(param, "10^9", "10^{super 9}");
param = tranwrd(param, "10^12", "10^{super 12}");
param = tranwrd(param, "-"||trim(parcat1), "");
/*avisit=tranwrd(avisit,"Screening Day -28 to -1","Screening");
avisit=tranwrd(avisit,"Post Study Day 21/Early Termination","Post Study Day 21");*/
/*   anrind = strip(substr(anrind,1, 1));*/
/*if ANRLO and ANRHI ne . then do;*/
/*range=strip(put(ANRLO,best.))||" - "||strip(put(ANRHI,best.));*/
/*end;*/
if visityp = 'Unscheduled' then avisit = 'Unscheduled ' || strip(avisit); 
run;

proc sort data=lb; 
by parcat1n parcat1 param anrange subjid avisitn avisit  adtm lbdtc avalc anrind lbclsig; 
run;

data lb_;
	set lb;
	by parcat1n parcat1 param anrange subjid avisitn avisit  adtm lbdtc avalc anrind lbclsig; 
	if first.parcat1 then pg=1;
	else pg+1;
run;

proc sort data=lb_; 
by parcat1n parcat1 pg param anrange subjid avisitn avisit adtm lbdtc avalc anrind lbclsig; 
run;

/*data check;*/
/* set lb(keep=parcat1n parcat1 param range usubjid subjid adtm lbdtc avisitn avisit visityp avalc anrind lbclsig);*/
/* by parcat1n parcat1 param range subjid avisitn avisit visityp adtm lbdtc avalc anrind lbclsig;*/
/* blnk=1;*/
/* output;*/
/* if first.parcat1 then do;*/
/*  array allchar {*} param range subjid lbdtc avisit visityp avalc anrind lbclsig;*/
/*  drop i;*/
/*  do i=1 to dim(allchar); allchar{i}=' '; end;*/
/*  blnk=0;*/
/*  output; end;*/
/*run;*/
/**/
/*data check1;*/
/* set check;*/
/*  if param eq '' then param=parcat1;*/
/*run;*/
/**/
/*%page_cut(in=check1,sort=parcat1n parcat1 blnk param range subjid avisitn avisit visityp adtm lbdtc avalc anrind lbclsig,*/
/*          columns=, width=,  newpage=, nocut=parcat1,skip=param,pagesize=22,out=lb1);*/


%page_cut(in=lb_,sort=parcat1n parcat1 pg param anrange subjid avisitn avisit adtm lbdtc avalc anrind lbclsig,
          columns=, width=,  newpage=, nocut=,skip=param,pagesize=21,out=lb1);

data lb1; set lb1; page0=page; run;

proc sort data=lb1 out=lb2(keep=page page0 parcat1n parcat1 param anrange usubjid subjid avisitn avisit adtm lbdtc avalc anrind lbclsig); 
by page page0 parcat1n parcat1 param anrange subjid avisitn avisit adtm lbdtc avalc anrind lbclsig; 
run;

data addpar(keep=page page0 parcat1n parcat1 param rename=(param=param0));
set lb2;
by page page0 parcat1n parcat1 param anrange subjid avisitn avisit adtm lbdtc avalc anrind lbclsig; 
if first.parcat1 then output addpar;
run;

data lb3;
set lb2 addpar;
run;

data lb3;
set lb3;
dummy='';
if parcat1 ne '' then parcat1=TRIM(parcat1) || "/" || TRIM(dummy);run;

data lb3; set lb3;
if param0=" " then do; param0=param; end; 
if param=" " then do; paramd=parcat1; end;
if paramd=" " then do; paramd=param; end;
run;

proc sort data=lb3; by page page0 parcat1n parcat1 param param0 paramd anrange subjid avisitn avisit  adtm lbdtc avalc anrind lbclsig; run;

proc sql noprint;
 select count(*)
 into :doit
 from lb3;
quit;

title3 " ";
title4 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..LB.&bn..rtf"; 

%empty_ds_fix(lb3);
%open_rtf;

title5 "Listing of &ttt On-Study Laboratory Values";
title6 "(Safety Population)";

footnote3 j=l "Note: Abnormal values are determined by applying the reference ranges to the results as reported by the external laboratory analysis.";
footnote4 j=l "[1] NCS:  Not Clinically Significant   /   CS: Clinically Significant / RPT: Repeated.";
footnote4 j=l "&ftnote";

proc report data=lb3 nowd split='~' missing out=dummy;
 column page page0 parcat1n parcat1 param param0 paramd anrange usubjid subjid avisitn avisit adtm lbdtc avalc anrind lbclsig;
 define page       / " " order order=internal noprint;
 define page0      / " " order order=internal noprint;
 define parcat1n   / " " order order=internal noprint;
 define parcat1    / " " order order=internal noprint;
 define param      / " " order order=internal noprint;
 define param0      / " " order order=internal noprint;

 define paramd     / "Category/~Parameter (Unit)" order order=internal style(column)=[cellwidth=2.6in just=left] style(header)=[just=left];
 define anrange      / "Reference Range" order order=internal style(column)=[cellwidth=1.2in just=left] style(header)=[just=l];
 define usubjid      / " " order order=internal noprint;
 define subjid     / "Subject ID" order order=internal style(column)=[cellwidth=0.9in just=left] style(header)=[just=l];
 define avisitn    / " " order order=internal noprint;
 define avisit     / "Visit" order order=internal style(column)=[cellwidth=1.2in just=left] style(header)=[just=left];
 *define visityp      / " " order order=internal noprint;
 define adtm      / " " order order=internal noprint;

 define lbdtc      / "Date / Time" order order=internal style(column)=[cellwidth=1.6in just=left] style(header)=[just=left];
 define avalc      / "Value" order style(column)=[cellwidth=0.7in just=l] style(header)=[just=l];
 define anrind     / "Flag" order style(column)=[cellwidth=0.6in just=left] style(header)=[just=left] ;
 define lbclsig    / "Assessment [1]" order style(column)=[cellwidth=0.95in just=left] style(header)=[just=left];

 break after page   / page;

 compute before page0 ;
 line " ";
 endcomp;

 compute after param0;
 line " ";
 endcomp;

 
 compute after avisit ;
 line " ";
 endcomp;

 %if &doit EQ 0 %then %do;
   compute after page;
    line "&emp";
   endcomp;
 %end;

run;

%close_rtf;
%arrange_rtf;

%mend print_listings;

%print_listings(program=T14_03_04_04_LB_ABN, disname=14.3.4.4, idisname=14.3.4.4, ds_list=ADLB, where=/*%str(LBCLSIG in ("NCS" "TBC" "CS"))*/, bn=ABN, ttt=Abnormal, 
	ftnote=%str([1] NCS:  Not Clinically Significant   /   CS: Clinically Significant ), emp=%str(No Abnormal Laboratory Values Were Measured.));
%clrw;




